package com.example.springcloudjwtserver.user.controller;

import com.example.springcloudjwtserver.user.domain.AdminUser;
import com.example.springcloudjwtserver.user.service.AdminUserService;
import com.example.springcloudjwtserver.util.JwtUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class UserController {
    Logger logger = LoggerFactory.getLogger(UserController.class);


    private AdminUserService adminUserService;
    public UserController(AdminUserService adminUserService) {
        this.adminUserService = adminUserService;
    }

    @RequestMapping("/index")
    public String index() {
        return "index";
    }


    @GetMapping("/login")
    public ResponseEntity login(@RequestParam("userName") String userName, @RequestParam("pwd") String pwd) {

        AdminUser adminUser = adminUserService.login(userName,pwd);
        if (adminUser == null) {
            return new ResponseEntity<>("账号密码错误！",HttpStatus.OK);
        }
        String jwt = JwtUtil.generateJwt(adminUser);
        return new ResponseEntity<>(jwt,HttpStatus.OK);
    }

    @GetMapping("/verify")
    public ResponseEntity verify(String jwt) {

        logger.error("jwt: {}",jwt);
      boolean flag = adminUserService.verify(jwt);
        if (!flag) {
            return new ResponseEntity<>("jwt验证失败！",HttpStatus.OK);
        }
        return new ResponseEntity<>("ok",HttpStatus.OK);
    }


}
